有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法启动selenium。java中的WebDriver firefox在一个没有UI的盒子中

我有一个类似于:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

class Gecko { 


...
FirefoxBinary ffB = new FirefoxBinary();
ffB.setEnvironmentProperty("DISPLAY", ":10");
...
options.setBinary(ffB);
WebDriver driver = new FirefoxDriver(options);

然后,我使用以下命令启动虚拟帧缓冲区:

Xvfb :10 -screen 0 1024x768x24 &

但当我使用Selenium时:

java -cp .:selenium-server-standalone-3.5.0.jar Gecko

以下问题显示:

1508364524466 geckodriver::marionette INFO Starting browser /usr/bin/firefox with args ["-marionette"]
Error: GDK_BACKEND does not match available displays
Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused

哪一个可能是原因?用户权限?防火墙?当前框未安装任何桌面环境


共 (1) 个答案

  1. # 1 楼答案

    如果显示错误:GDK_后端与可用显示不匹配,请安装pyvirtualdisplay:

    pip install pyvirtualdisplay selenium
    You might need xvfb too:
    
    sudo apt-get install xvfb
    Then try adding this code:
    
    from pyvirtualdisplay import Display
    display = Display(visible=0, size=(800, 600))
    display.start()
    Full example:
    
    from pyvirtualdisplay import Display
    from selenium import webdriver
    
    display = Display(visible=0, size=(800, 600))
    display.start()
    
    browser = webdriver.Firefox()
    browser.get('http://www.python.org')
    
    browser.close()
    display.stop()